package cn.ycxy.lbl.posts.controller;

import cn.ycxy.lbl.common.exception.BusinessException;
import cn.ycxy.lbl.common.exception.enums.ResponseEnum;
import cn.ycxy.lbl.common.restful.R;
import cn.ycxy.lbl.pojo.front.domain.LblCsCategory;
import cn.ycxy.lbl.pojo.front.domain.LblCsFeedback;
import cn.ycxy.lbl.pojo.front.domain.SysNotice;
import cn.ycxy.lbl.posts.service.ICategoryService;
import cn.ycxy.lbl.posts.service.IFeedbackService;
import cn.ycxy.lbl.posts.service.ISysNoticeService;
import cn.ycxy.lbl.posts.utils.SecurityContextUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "首页模块内容")
@RestController
@RequestMapping("/front")
public class FrontController {

    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private IFeedbackService feedbackService;

    @ApiOperation("获取轮播图")
    @GetMapping("/category")
    public R<LblCsCategory> getCategory(){
        QueryWrapper<LblCsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time", "update_time");
        queryWrapper.last("limit 1");
        return R.ok(categoryService.getOne(queryWrapper));
    }

    @ApiOperation("获取公告")
    @GetMapping("/notice")
    public R<SysNotice> getSysNotice(){
        QueryWrapper<SysNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_type", 2);
        queryWrapper.orderByDesc("create_time", "update_time");
        queryWrapper.last("limit 1");
        return R.ok(sysNoticeService.getOne(queryWrapper));
    }

    @ApiOperation("提交意见反馈")
    @PostMapping("/feedback")
    public R<Void> feedback(@Validated @RequestBody LblCsFeedback feedback){
        Long userId = SecurityContextUtils.getUserId();
        feedback.setUserId(userId.intValue());
        if(!feedbackService.save(feedback)){
            throw new BusinessException(ResponseEnum.FEEDBACK_SAVE_FAILED);
        }
        return R.ok();
    }

}
